// takeEvery  处理监听
// call、 fork 调用异步操作
// put dispatch的封装

// import { take, call, fork, put, takeEvery } from 'redux-saga/effects';
import { take, call, fork, put } from '../../yl-redux-saga/effects';
import LoginService from '../../service/login';
import { LOGIN_FAILURE, LOGIN_SAGA, LOGIN_SUCCESS, REQUEST } from './const';

// worker saga
function* loginHandle(action) {
  //先修改store中的loading为true
  yield put({ type: REQUEST });
  try {
    // 现获取基本信息
    const res1 = yield call(LoginService.login, action.payload);
    // 再获取详细信息
    const res2 = yield call(LoginService.getMoreUserInfo, res1);

    yield put({ type: LOGIN_SUCCESS, payload: res2 });
  } catch (err) {
    yield put({ type: LOGIN_FAILURE, payload: err });
  }
}

// watcher saga
function* loginSaga() {
  while (true) {
    console.log('hahahh');
    const action = yield take(LOGIN_SAGA);
    console.log('action==>', action);
    yield fork(loginHandle, action);
  }
}

// watcher saga，与上面写法等价
// function* loginSaga() {
//   yield takeEvery(LOGIN_SAGA, loginHandle);
// }

export default loginSaga;
